import { defineStore } from 'pinia'

export const useAppStore = defineStore({
    id: 'app',
    state: () => {
        return {
            name: '张三',
            loading:false,
            tbPositionVisible:false,
            tbPositionTkVisible:false,
            tbWiringTkVisible:false,
            meterWiringIds:[],
            currentPlanId:'',
            currentEpitope:0,
            epitopeList:[],
            organStructure:[
                {
                    id: 1,
                    pname: "系统设置",
                    pid:0,
                    position: "",
                    meterModel: "",
                    meterType: "",
                    wiringType: "",
                    voltage: "",
                    current: "",
                    meterAddr: "",
                    userAddr:"",
                    ptRatio: "",
                    ctRatio: "",
                    installAddr: "",
                    children: [],
                    //type:1,//1台区；2台区下终端；3台区下总表；4台区下用户表；5转变用户；6专变下终端；7专变下用户表；8用户表
                },
            ],
            tableFault:[],
            tableHeader:[],
            tableStudent:[],
            currentEleType:[],
            lossParameters:[],
            tempData:[],//选中当前发送表地址
            currentAccountingData:[],//当前核算方案
            monthMeterReading:[],//抄表底数
            receivedData:[],//核收数据
            calculationData:[],//计算数据
            powerEle:'',//线损参数
            tableVoltage:[],
            meterCurrent:[],
            readerDataArr:[],
            tableFaultSetting:[],//表位故障设置
            tableFaultParm:[],//表位故障接口传参
            isSuccess:false,//全部故障-接口是否成功返回
            faultLoading:false,
            meterIds:[],
        }
    },
    getters: {},
    actions: {
        setCurrentData(data) {
            this.organStructure[0].children.push(data)
        },
        delCurrentData(id) {
            let count=0;
            function removeNodeById(nodes, id, epitopeCounter) {  
                for (let i = 0; i < nodes.length; i++) {  
                    const node = nodes[i];  
                    if (node.id === id) {  
                        // 找到匹配的节点，删除它并减少 epitopeCounter  
                        nodes.splice(i, 1);   
                        return; // 找到后退出循环  
                    }  
                    if (Array.isArray(node.children)) {  
                        // 递归调用以处理嵌套的子节点  
                        removeNodeById(node.children, id, epitopeCounter);
                        count +=node.children.length;
                        // console.log(count)
                    }  
                }  
            }  
            // 使用函数  
            let num = this.organStructure.length; // 假设每个节点代表一个 epitope  
            removeNodeById(this.organStructure, id,num);      
        },
        setEpitope(){
            this.currentEpitope++
        }
    },
    persist: {
        enabled: true,
        encryptionKey: 'app',
        strategies: [{
            key: 'app',
            storage: sessionStorage,
        }, ],
    },
})